home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / MacPerl Source ƒ / Perl5 / t / lib / db-btree.t < prev    next >
Encoding:
Text File  |  1995-10-30  |  8.9 KB  |  355 lines  |  [TEXT/MPS ]

  1. #!./perl
  2.  
  3. BEGIN {
  4.     chdir 't' if -d 't';
  5.     chdir '::' unless -d 'lib';
  6. #    @INC = '../lib';
  7.     @INC = '::lib';
  8.     require Config; import Config;
  9.     if ($Config{'extensions'} !~ /\bDB_File\b/) {
  10.     print "1..0\n";
  11.     exit 0;
  12.     }
  13. }
  14.  
  15. use DB_File; 
  16. use Fcntl;
  17.  
  18. print "1..73\n";
  19.  
  20. $Dfile = "Op.db-btree";
  21. unlink $Dfile;
  22.  
  23. # umask(0);
  24.  
  25. # Check the interface to BTREEINFO
  26.  
  27. $dbh = TIEHASH DB_File::BTREEINFO ;
  28. print (($dbh->{flags} == undef) ? "ok 1\n" : "not ok 1\n") ;
  29. print (($dbh->{cachesize} == undef) ? "ok 2\n" : "not ok 2\n") ;
  30. print (($dbh->{psize} == undef) ? "ok 3\n" : "not ok 3\n") ;
  31. print (($dbh->{lorder} == undef) ? "ok 4\n" : "not ok 4\n") ;
  32. print (($dbh->{minkeypage} == undef) ? "ok 5\n" : "not ok 5\n") ;
  33. print (($dbh->{maxkeypage} == undef) ? "ok 6\n" : "not ok 6\n") ;
  34. print (($dbh->{compare} == undef) ? "ok 7\n" : "not ok 7\n") ;
  35. print (($dbh->{prefix} == undef) ? "ok 8\n" : "not ok 8\n") ;
  36.  
  37. $dbh->{flags} = 3000 ;
  38. print ($dbh->{flags} == 3000 ? "ok 9\n" : "not ok 9\n") ;
  39.  
  40. $dbh->{cachesize} = 9000 ;
  41. print ($dbh->{cachesize} == 9000 ? "ok 10\n" : "not ok 10\n") ;
  42. #
  43. $dbh->{psize} = 400 ;
  44. print (($dbh->{psize} == 400) ? "ok 11\n" : "not ok 11\n") ;
  45.  
  46. $dbh->{lorder} = 65 ;
  47. print (($dbh->{lorder} == 65) ? "ok 12\n" : "not ok 12\n") ;
  48.  
  49. $dbh->{minkeypage} = 123 ;
  50. print (($dbh->{minkeypage} == 123) ? "ok 13\n" : "not ok 13\n") ;
  51.  
  52. $dbh->{maxkeypage} = 1234 ;
  53. print ($dbh->{maxkeypage} == 1234 ? "ok 14\n" : "not ok 14\n") ;
  54.  
  55. $dbh->{compare} = 1234 ;
  56. print ($dbh->{compare} == 1234 ? "ok 15\n" : "not ok 15\n") ;
  57.  
  58. $dbh->{prefix} = 1234 ;
  59. print ($dbh->{prefix} == 1234 ? "ok 16\n" : "not ok 16\n") ;
  60.  
  61. # Check that an invalid entry is caught both for store & fetch
  62. eval '$dbh->{fred} = 1234' ;
  63. print ($@ eq '' ? "ok 17\n" : "not ok 17\n") ;
  64. eval '$q = $dbh->{fred}' ;
  65. print ($@ eq '' ? "ok 18\n" : "not ok 18\n") ;
  66.  
  67. # Now check the interface to BTREE
  68.  
  69. print (($X = tie(%h, DB_File,$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ? "ok 19\n" : "not ok 19");
  70.  
  71. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
  72.    $blksize,$blocks) = stat($Dfile);
  73. print (($mode & 0777) == 0640 ? "ok 20\n" : "not ok 20\n");
  74.  
  75. while (($key,$value) = each(%h)) {
  76.     $i++;
  77. }
  78. print (!$i ? "ok 21\n" : "not ok 21\n");
  79.  
  80. $h{'goner1'} = 'snork';
  81.  
  82. $h{'abc'} = 'ABC';
  83. print ($h{'abc'} == 'ABC' ? "ok 22\n" : "not ok 22\n") ;
  84. print (defined $h{'jimmy'} ? "not ok 23\n" : "ok 23\n");
  85.  
  86. $h{'def'} = 'DEF';
  87. $h{'jkl','mno'} = "JKL\034MNO";
  88. $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
  89. $h{'a'} = 'A';
  90.  
  91. #$h{'b'} = 'B';
  92. $X->STORE('b', 'B') ;
  93.  
  94. $h{'c'} = 'C';
  95.  
  96. #$h{'d'} = 'D';
  97. $X->put('d', 'D') ;
  98.  
  99. $h{'e'} = 'E';
  100. $h{'f'} = 'F';
  101. $h{'g'} = 'X';
  102. $h{'h'} = 'H';
  103. $h{'i'} = 'I';
  104.  
  105. $h{'goner2'} = 'snork';
  106. delete $h{'goner2'};
  107.  
  108.  
  109. # IMPORTANT - $X must be undefined before the untie otherwise the
  110. #             underlying DB close routine will not get called.
  111. undef $X ;
  112. untie(%h);
  113.  
  114.  
  115. # tie to the same file again
  116. print (($X = tie(%h,DB_File,$Dfile, O_RDWR, 0640, $DB_BTREE)) ? "ok 24\n" : "not ok 24\n");
  117.  
  118. # Modify an entry from the previous tie
  119. $h{'g'} = 'G';
  120.  
  121. $h{'j'} = 'J';
  122. $h{'k'} = 'K';
  123. $h{'l'} = 'L';
  124. $h{'m'} = 'M';
  125. $h{'n'} = 'N';
  126. $h{'o'} = 'O';
  127. $h{'p'} = 'P';
  128. $h{'q'} = 'Q';
  129. $h{'r'} = 'R';
  130. $h{'s'} = 'S';
  131. $h{'t'} = 'T';
  132. $h{'u'} = 'U';
  133. $h{'v'} = 'V';
  134. $h{'w'} = 'W';
  135. $h{'x'} = 'X';
  136. $h{'y'} = 'Y';
  137. $h{'z'} = 'Z';
  138.  
  139. $h{'goner3'} = 'snork';
  140.  
  141. delete $h{'goner1'};
  142. $X->DELETE('goner3');
  143.  
  144. @keys = keys(%h);
  145. @values = values(%h);
  146.  
  147. if ($#keys == 29 && $#values == 29) {print "ok 25\n";} else {print "not ok 25\n";}
  148.  
  149. while (($key,$value) = each(%h)) {
  150.     if ($key eq $keys[$i] && $value eq $values[$i] && $key gt $value) {
  151.     $key =~ y/a-z/A-Z/;
  152.     $i++ if $key eq $value;
  153.     }
  154. }
  155.  
  156. if ($i == 30) {print "ok 26\n";} else {print "not ok 26\n";}
  157.  
  158. @keys = ('blurfl', keys(h), 'dyick');
  159. if ($#keys == 31) {print "ok 27\n";} else {print "not ok 27\n";}
  160.  
  161. #Check that the keys can be retrieved in order
  162. $ok = 1 ;
  163. foreach (keys %h)
  164. {
  165.     ($ok = 0), last if defined $previous && $previous gt $_ ;
  166.     $previous = $_ ;
  167. }
  168. print ($ok ? "ok 28\n" : "not ok 28\n") ;
  169.  
  170. $h{'foo'} = '';
  171. print ($h{'foo'} eq '' ? "ok 29\n" : "not ok 29\n") ;
  172.  
  173. $h{''} = 'bar';
  174. print ($h{''} eq 'bar' ? "ok 30\n" : "not ok 30\n") ;
  175.  
  176. # check cache overflow and numeric keys and contents
  177. $ok = 1;
  178. for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
  179. for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
  180. print ($ok ? "ok 31\n" : "not ok 31\n");
  181.  
  182. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
  183.    $blksize,$blocks) = stat($Dfile);
  184. print ($size > 0 ? "ok 32\n" : "not ok 32\n");
  185.  
  186. @h{0..200} = 200..400;
  187. @foo = @h{0..200};
  188. print join(':',200..400) eq join(':',@foo) ? "ok 33\n" : "not ok 33\n";
  189.  
  190. # Now check all the non-tie specific stuff
  191.  
  192.  
  193. # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
  194. # an existing record.
  195.  
  196. $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
  197. print ($status == 1 ? "ok 34\n" : "not ok 34\n") ;
  198.  
  199. # check that the value of the key 'x' has not been changed by the 
  200. # previous test
  201. print ($h{'x'} eq 'X' ? "ok 35\n" : "not ok 35\n") ;
  202.  
  203. # standard put
  204. $status = $X->put('key', 'value') ;
  205. print ($status == 0 ? "ok 36\n" : "not ok 36\n") ;
  206.  
  207. #check that previous put can be retrieved
  208. $status = $X->get('key', $value) ;
  209. print ($status == 0 ? "ok 37\n" : "not ok 37\n") ;
  210. print ($value eq 'value' ? "ok 38\n" : "not ok 38\n") ;
  211.  
  212. # Attempting to delete an existing key should work
  213.  
  214. $status = $X->del('q') ;
  215. print ($status == 0 ? "ok 39\n" : "not ok 39\n") ;
  216. $status = $X->del('') ;
  217. print ($status == 0 ? "ok 40\n" : "not ok 40\n") ;
  218.  
  219. # Make sure that the key deleted, cannot be retrieved
  220. print (($h{'q'} eq undef) ? "ok 41\n" : "not ok 41\n") ;
  221. print (($h{''} eq undef) ? "ok 42\n" : "not ok 42\n") ;
  222.  
  223. undef $X ;
  224. untie %h ;
  225.  
  226. print (($X = tie(%h, DB_File,$Dfile, O_RDWR, 0640, $DB_BTREE )) ? "ok 43\n" : "not ok 43");
  227.  
  228. # Attempting to delete a non-existant key should fail
  229.  
  230. $status = $X->del('joe') ;
  231. print ($status == 1 ? "ok 44\n" : "not ok 44\n") ;
  232.  
  233. # Check the get interface
  234.  
  235. # First a non-existing key
  236. $status = $X->get('aaaa', $value) ;
  237. print ($status == 1 ? "ok 45\n" : "not ok 45\n") ;
  238.  
  239. # Next an existing key
  240. $status = $X->get('a', $value) ;
  241. print ($status == 0 ? "ok 46\n" : "not ok 46\n") ;
  242. print ($value eq 'A' ? "ok 47\n" : "not ok 47\n") ;
  243.  
  244. # seq
  245. # ###
  246.  
  247. # use seq to find an approximate match
  248. $key = 'ke' ;
  249. $value = '' ;
  250. $status = $X->seq($key, $value, R_CURSOR) ;
  251. print ($status == 0 ? "ok 48\n" : "not ok 48\n") ;
  252. print ($key eq 'key' ? "ok 49\n" : "not ok 49\n") ;
  253. print ($value eq 'value' ? "ok 50\n" : "not ok 50\n") ;
  254.  
  255. # seq when the key does not match
  256. $key = 'zzz' ;
  257. $value = '' ;
  258. $status = $X->seq($key, $value, R_CURSOR) ;
  259. print ($status == 1 ? "ok 51\n" : "not ok 51\n") ;
  260.  
  261.  
  262. # use seq to set the cursor, then delete the record @ the cursor.
  263.  
  264. $key = 'x' ;
  265. $value = '' ;
  266. $status = $X->seq($key, $value, R_CURSOR) ;
  267. print ($status == 0 ? "ok 52\n" : "not ok 52\n") ;
  268. print ($key eq 'x' ? "ok 53\n" : "not ok 53\n") ;
  269. print ($value eq 'X' ? "ok 54\n" : "not ok 54\n") ;
  270. $status = $X->del(0, R_CURSOR) ;
  271. print ($status == 0 ? "ok 55\n" : "not ok 55\n") ;
  272. $status = $X->get('x', $value) ;
  273. print ($status == 1 ? "ok 56\n" : "not ok 56\n") ;
  274.  
  275. # ditto, but use put to replace the key/value pair.
  276. $key = 'y' ;
  277. $value = '' ;
  278. $status = $X->seq($key, $value, R_CURSOR) ;
  279. print ($status == 0 ? "ok 57\n" : "not ok 57\n") ;
  280. print ($key eq 'y' ? "ok 58\n" : "not ok 58\n") ;
  281. print ($value eq 'Y' ? "ok 59\n" : "not ok 59\n") ;
  282.  
  283. $key = "replace key" ;
  284. $value = "replace value" ;
  285. $status = $X->put($key, $value, R_CURSOR) ;
  286. print ($status == 0 ? "ok 60\n" : "not ok 60\n") ;
  287. print ($key eq 'replace key' ? "ok 61\n" : "not ok 61\n") ;
  288. print ($value eq 'replace value' ? "ok 62\n" : "not ok 62\n") ;
  289. $status = $X->get('y', $value) ;
  290. print ($status == 1 ? "ok 63\n" : "not ok 63\n") ;
  291.  
  292. # use seq to walk forwards through a file 
  293.  
  294. $status = $X->seq($key, $value, R_FIRST) ;
  295. print ($status == 0 ? "ok 64\n" : "not ok 64\n") ;
  296. $previous = $key ;
  297.  
  298. $ok = 1 ;
  299. while (($status = $X->seq($key, $value, R_NEXT)) == 0)
  300. {
  301.     ($ok = 0), last if ($previous cmp $key) == 1 ;
  302. }
  303.  
  304. print ($status == 1 ? "ok 65\n" : "not ok 65\n") ;
  305. print ($ok == 1 ? "ok 66\n" : "not ok 66\n") ;
  306.  
  307. # use seq to walk backwards through a file 
  308. $status = $X->seq($key, $value, R_LAST) ;
  309. print ($status == 0 ? "ok 67\n" : "not ok 67\n") ;
  310. $previous = $key ;
  311.  
  312. $ok = 1 ;
  313. while (($status = $X->seq($key, $value, R_PREV)) == 0)
  314. {
  315.     ($ok = 0), last if ($previous cmp $key) == -1 ;
  316.     #print "key = [$key] value = [$value]\n" ;
  317. }
  318.  
  319. print ($status == 1 ? "ok 68\n" : "not ok 68\n") ;
  320. print ($ok == 1 ? "ok 69\n" : "not ok 69\n") ;
  321.  
  322.  
  323. # check seq FIRST/LAST
  324.  
  325. # sync
  326. # ####
  327.  
  328. $status = $X->sync ;
  329. print ($status == 0 ? "ok 70\n" : "not ok 70\n") ;
  330.  
  331.  
  332. # fd
  333. # ##
  334.  
  335. $status = $X->fd ;
  336. print ($status != 0 ? "ok 71\n" : "not ok 71\n") ;
  337.  
  338.  
  339. undef $X ;
  340. untie %h ;
  341.  
  342. unlink $Dfile;
  343.  
  344. # Now try an in memory file
  345. print (($Y = tie(%h, DB_File,undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ? "ok 72\n" : "not ok 72");
  346.  
  347. # fd with an in memory file should return failure
  348. $status = $Y->fd ;
  349. print ($status == -1 ? "ok 73\n" : "not ok 73\n") ;
  350.  
  351. undef $Y ;
  352. untie %h ;
  353.  
  354. exit ;
  355.